package com.hjy.mybatisgenerator.JpaUtil.code;

import com.hjy.mybatisgenerator.JpaUtil.db.DBUtil;
import com.hjy.mybatisgenerator.JpaUtil.util.Constant;
import com.hjy.mybatisgenerator.JpaUtil.util.GenUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * do层，直接连接数据库
 * Created by hjy on 2018/11/23.
 */
public class GenDoCode {
    /*
     * 构造函数
     */
    public GenDoCode(String tablename,String describe) {
        // 首字母大写
        String bigTableName = DBUtil.bigTableName +"Do";
        // 首字母小写
        String smallTableName = DBUtil.smallTableName +"Do";

        // 开始生成Do代码
        String dtoCode = dtoCode(tablename,bigTableName,smallTableName,describe);
        try {
            File directory = new File("");
            String outputPath = directory.getAbsolutePath() + "/src/main/java/"+ Constant.PACKAGE_OUTPATH_DO.replace(".", "/") + "/" + bigTableName + ".java";
            FileWriter fw = new FileWriter(outputPath);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(dtoCode);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("【"+tablename+"】表【Do数据传输对象】生成完毕！");
    }

    /**
     * 功能：生成Do主体代码
     * @param tablename
     * @param bigTableName
     * @param smallTableName
     * @param describe
     * @return
     */
    private String dtoCode(String tablename,String bigTableName,String smallTableName,String describe) {
        StringBuffer sb = new StringBuffer();

        // 导包
        sb.append("package " + Constant.PACKAGE_OUTPATH_DO + ";\r\n");
        sb.append("\r\n");
        sb.append("import com.baomidou.mybatisplus.annotations.TableField;\r\n");
        sb.append("import com.baomidou.mybatisplus.annotations.TableName;\r\n");
        sb.append("import com.lishiots.cloud.ots.core.domain.MyBatisPlusBaseModel;\r\n");
        sb.append("\r\n");
        sb.append("import java.math.BigDecimal;\r\n");
        sb.append("import java.util.Date;\r\n");


        // 注释部分
        sb.append("/**\r\n");
        sb.append(" * @CopyRight: 版权所有\r\n");
        sb.append(" * @author " + Constant.AUTHORNAME +  "\r\n");
        sb.append(" * @Description:  " + describe +  "\r\n");
        sb.append(" * @Date: " + GenUtil.getDate() +  "\r\n");
        sb.append(" */ \r\n");

        // 实体部分
        sb.append("@TableName(value = \"" + tablename + "\") \r\n");
        sb.append("public class " + bigTableName + " extends MyBatisPlusBaseModel<"+bigTableName+"> {\r\n\r\n");

        //生成所有属性
        processAllAttrs(sb);
        //生成所有方法(get set方法)
        processAllMethod(sb);
        sb.append("}\r\n");

        return sb.toString();
    }

    /**
     * 功能：生成所有属性
     * @param sb
     */
    private void processAllAttrs(StringBuffer sb) {
        for (int i = 0; i < DBUtil.colnames.length; i++) {
            // 处理列名
            String bigColname = GenUtil.strChange(DBUtil.colnames[i]);

            sb.append("\t/**\r\n");
            sb.append("\t * " + DBUtil.remarks[i] +  "\r\n");
            sb.append("\t */ \r\n");

            sb.append("\t@TableField(value = \"" + DBUtil.colnames[i] + "\")\r\n");
            sb.append("\tprivate " + GenUtil.sqlType2JavaType(DBUtil.colTypes[i]) + " " + bigColname + ";\r\n\r\n");
        }
    }

    /**
     * 功能：生成所有方法
     * @param sb
     */
    private void processAllMethod(StringBuffer sb) {

        for (int i = 0; i < DBUtil.colnames.length; i++) {
            // 处理列名
            String bigColname = GenUtil.strChange(DBUtil.colnames[i]);
            sb.append("\tpublic " + GenUtil.sqlType2JavaType(DBUtil.colTypes[i]) + " get" + GenUtil.initcap(bigColname) + "(){\r\n");
            sb.append("\t\treturn " + bigColname + ";\r\n");
            sb.append("\t}\r\n\r\n");
            sb.append("\tpublic void set" + GenUtil.initcap(bigColname) + "("+ GenUtil.sqlType2JavaType(DBUtil.colTypes[i]) + " " + bigColname + "){\r\n");
            sb.append("\t\tthis." + bigColname + " = " + bigColname + ";\r\n");
            sb.append("\t}\r\n\r\n");
        }
    }

}
